//
//  ZHLEESearchView.swift
//  ZHLEEBlog_swift_pro
//
//  Created by ZHLEEBlander on 15/10/11.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

class ZHLEESearchView: UIView,UITextFieldDelegate {

    @IBOutlet weak var Cancel: UIButton!
    @IBOutlet weak var constraint: NSLayoutConstraint!
    
    @IBOutlet weak var searchField: UITextField!
    
    class func searchView()->ZHLEESearchView {
        
        return NSBundle.mainBundle().loadNibNamed("ZHLEESearchView", owner: nil, options: nil).last as! ZHLEESearchView
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        searchField.layer.borderWidth = 1
        searchField.layer.borderColor = UIColor.purpleColor().CGColor
        searchField.layer.cornerRadius = 5
        
        searchField.delegate = self
        
        //设置输入框的leftView
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        leftView.size = CGSizeMake(height, height)
        //设置leftView的显示模式为居中,大小按照原样的大小
        leftView.contentMode = UIViewContentMode.Center
        
        searchField.leftView = leftView
        //设置leftView的显示模式
        searchField.leftViewMode = UITextFieldViewMode.Always
        
    }
    
    @IBAction func cancelDidClick(sender: AnyObject) {
        
        searchField.text = ""
        endEditing(true)
        constraint.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    
    // MARK: - UITextFieldDelegate
    func textFieldDidBeginEditing(textField: UITextField) {
        printLog("开始编辑")
        printLog("xxx")
        
        self.constraint.constant = self.Cancel.width
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    func textFieldDidEndEditing(textField: UITextField) {
        printLog("结束编辑")
    }
}
